package com.shenmazong.customer.service.impl;

import com.shenmazong.customer.service.IAskService;
import com.shenmazong.customer.vo.OnlineUserInfo;
import com.shenmazong.utils.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Set;

/**
 * @author 军哥
 * @version 1.0
 * @description: TODO
 * @date 2022/4/11 11:01
 */

@Service
public class AskServiceImpl implements IAskService {

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    private final  String onlineAskKey = "customer_websocket_online_users";
    private final  String onlineReplayKey = "customer_websocket_online_services";


    @Override
    public ResultResponse getAskUserList() {

        ArrayList<OnlineUserInfo> list = new ArrayList<>();

        Set<Object> keys = redisTemplate.opsForHash().keys(onlineAskKey);
        keys.forEach(item -> {
            String sessionId = (String) item;
            OnlineUserInfo user = (OnlineUserInfo)redisTemplate.opsForHash().get(onlineAskKey, sessionId);
            if(user != null) {
                list.add(user);
            }
        });

        return ResultResponse.SUCCESS(list);
    }

    @Override
    public ResultResponse getReplyUserList() {
        ArrayList<OnlineUserInfo> list = new ArrayList<>();

        Set<Object> keys = redisTemplate.opsForHash().keys(onlineReplayKey);
        keys.forEach(item -> {
            String sessionId = (String) item;
            OnlineUserInfo user = (OnlineUserInfo)redisTemplate.opsForHash().get(onlineReplayKey, sessionId);
            if(user != null) {
                list.add(user);
            }
        });

        return ResultResponse.SUCCESS(list);
    }
}
